/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package simcreature;

import java.awt.Component;
import java.util.*;
import javax.swing.*;
import java.awt.GridLayout; //imports GridLayout library
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 *
 * @author Fari1Mat
 */
public class Board implements ActionListener{

    JMenuBar menuBar = new JMenuBar();
    JMenu fileMenu = new JMenu("File");
    JMenu helpMenu = new JMenu("Help");



    JFrame frame = new JFrame(); //creates frame
    static JLabel[][] grid; //names the grid of buttons
    static Node[][] board;
    static int size;
    ImageIcon icon = new ImageIcon(Board.class.getResource("resources/grass.gif"));
    static LinkedList <Creature> creatureList;
    static LinkedList <Vegetation> vegetationList;


    Board(int size) { //constructor
        creatureList = new <Creature> LinkedList();
        vegetationList = new <Vegetation> LinkedList();
        board = new Node[size][size]; //creates board array
        //Node board[][] = new Node[size][size]; //creates board array
        frame.setLayout(new GridLayout(size,size)); //set layout
        grid = new JLabel[size][size]; //allocate the size of grid

        for (int x = 0; x < size; x++) {
                for (int y = 0; y < size; y++) {
                //board[x][y].label.setIcon(icon);
                    grid[x][y] = new JLabel(icon); //creates new label
                //frame.add(board[x][y].label);
                    frame.add(grid[x][y]); //adds label to grid
                }
            }

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        makeMenuBar();
        frame.setJMenuBar(menuBar);
        frame.pack(); //sets appropriate size for frame
        frame.setVisible(true); //makes frame visible
        
    }

    static void update(int x, int y, ImageIcon icon) {
        //board[x][y].label.setIcon(icon);
        grid[y][x].setIcon(icon);
    }

    void makeMenuBar(){
        menuBar.add(fileMenu);
        menuBar.add(helpMenu);

        JMenuItem newGameItem = new JMenuItem("New Game");
        newGameItem.addActionListener(this);
        newGameItem.setActionCommand("new");
        fileMenu.add(newGameItem);

        JMenuItem saveItem = new JMenuItem("Save Game");
        saveItem.addActionListener(this);
        saveItem.setActionCommand("save");
        fileMenu.add(saveItem);

        JMenuItem loadItem = new JMenuItem("Load Game");
        loadItem.addActionListener(this);
        loadItem.setActionCommand("load");
        fileMenu.add(loadItem);

        JMenuItem exitItem = new JMenuItem("Exit");
        exitItem.addActionListener(this);
        exitItem.setActionCommand("exit");
        fileMenu.add(exitItem);

        JMenuItem aboutItem = new JMenuItem("About");
        aboutItem.addActionListener(this);
        aboutItem.setActionCommand("about");
        helpMenu.add(aboutItem);

    }

    public void actionPerformed(ActionEvent e){
        if ("exit".equals(e.getActionCommand())){
            System.exit(0);
        }else if("about".equals(e.getActionCommand())){
            System.out.println("Show About");
        }else if("new".equals(e.getActionCommand())){
            newGame(size-1);
        }else if("save".equals(e.getActionCommand())){
            saveGame();
        }else if("load".equals(e.getActionCommand())){
            loadGame();
        }
    }

    void populate(LinkedList <Creature> cList){
        Iterator<Creature> iter1 = cList.iterator();
        while(iter1.hasNext()){
            Creature c = iter1.next();
            int x = c.getX();
            int y = c.getY();
            update(x, y, c.getIcon());
        }

    }

    void newGame(int newSize){
        frame.removeAll();
    }

    void saveGame(){
        System.out.println("Save game according to chad's slides");
        System.out.println("Pop up a window saying success or failure");
    }

    void loadGame(){
        System.out.println("Read in savegame file");
        System.out.println("Set board attributes to ones from file");
        System.out.println("Call populate with creatureList from file");
    }
}